package ro.fortech.peaa.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.springframework.web.servlet.view.xslt.AbstractXsltView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import ro.fortech.peaa.domain.Employee;

/**
 * Creates the source for xslt.
 * @author robert
 *
 */
@SuppressWarnings("deprecation")
public class EmployeesView extends AbstractXsltView {
	@SuppressWarnings("unchecked")
	public Source createXsltSource(Map model, String rootName, HttpServletRequest
	        request, HttpServletResponse response) {
    
		Document document = null;
		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	    Element root = document.createElement(rootName);
	    
	    Double xx = model.get("xx") != null ? (Double) model.get("xx") : null;
	    Double yy = model.get("yy") != null ? (Double) model.get("yy") : null;
	    Double averageSalary = (Double) model.get("average_salary");
	    List<Employee> employees = (List<Employee>) model.get("employees");
	    for (Iterator<Employee> it = employees.iterator(); it.hasNext();) {
	        Employee nextEmployee = (Employee) it.next();
	        
	        Element employeeNode = document.createElement("employee");
	        
	        // employee id
	        Element idNode = document.createElement("id");
	        Text textNode = document.createTextNode(nextEmployee.getId().toString());
	        idNode.appendChild(textNode);
	        employeeNode.appendChild(idNode);
	        
	        // first name
	        Element firstNameNode = document.createElement("first_name");
	        textNode = document.createTextNode(nextEmployee.getFirstName());
	        firstNameNode.appendChild(textNode);
	        employeeNode.appendChild(firstNameNode);
	        
	        // last name
	        Element lastNameNode = document.createElement("last_name");
	        textNode = document.createTextNode(nextEmployee.getLastName());
	        lastNameNode.appendChild(textNode);
	        employeeNode.appendChild(lastNameNode);      
	        
	        // email
	        Element emailNode = document.createElement("email");
	        textNode = document.createTextNode(nextEmployee.getEmail());
	        emailNode.appendChild(textNode);
	        employeeNode.appendChild(emailNode);
	        
	        // phone_number
	        Element phoneNode = document.createElement("phone");
	        textNode = document.createTextNode(nextEmployee.getPhoneNumber());
	        phoneNode.appendChild(textNode);
	        employeeNode.appendChild(phoneNode);
	        
	        // hire_date
	        Element hireDateNode = document.createElement("hire_date");
	        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        textNode = document.createTextNode(dateFormat.format(nextEmployee.getHireDate()));
	        hireDateNode.appendChild(textNode);
	        employeeNode.appendChild(hireDateNode);
	        
	        // job_id
	        Element jobIdNode = document.createElement("job_id");
	        textNode = document.createTextNode(nextEmployee.getJobId());
	        jobIdNode.appendChild(textNode);
	        employeeNode.appendChild(jobIdNode);

	        // salary
	        Element salaryNode = document.createElement("salary");
	        textNode = document.createTextNode(nextEmployee.getSalary().toString());
	        salaryNode.appendChild(textNode);
	        employeeNode.appendChild(salaryNode);
	        
	        // commision_pct
	        Element commisionPctNode = document.createElement("commission_pct");
	        textNode = document.createTextNode(nextEmployee.getCommissionPct().toString());
	        commisionPctNode.appendChild(textNode);
	        employeeNode.appendChild(commisionPctNode);
	        
	        // manager_id
	        Element managerIdNode = document.createElement("manager_id");
	        textNode = document.createTextNode(nextEmployee.getManagerId().toString());
	        managerIdNode.appendChild(textNode);
	        employeeNode.appendChild(managerIdNode);
	        
	        // department_id
	        Element departmentIdNode = document.createElement("department_id");
	        textNode = document.createTextNode(nextEmployee.getDepartmentId().toString());
	        departmentIdNode.appendChild(textNode);
	        employeeNode.appendChild(departmentIdNode);
	        
	        if (nextEmployee.getSalary() >= averageSalary) {
		        if ((null != xx) && ((nextEmployee.getSalary() - averageSalary) * 100 / averageSalary > xx)) {
		        	employeeNode.setAttribute("color", "green");
		        }
	        }
	        
	        if (nextEmployee.getSalary() <= averageSalary) {
		        if ((null != yy) && ((averageSalary - nextEmployee.getSalary()) * 100 / averageSalary > yy)) {
		        	employeeNode.setAttribute("color", "red");
		        }
	        }
	        root.appendChild(employeeNode);
	    }
	    
	    Element averageSalaryNode = document.createElement("average_salary");
	    Text textNode = document.createTextNode(averageSalary.toString());
	    averageSalaryNode.appendChild(textNode);
	    root.appendChild(averageSalaryNode);
	    
	    Element nrOfEmployeesNode = document.createElement("count");
	    textNode = document.createTextNode(String.valueOf(employees.size()));
	    nrOfEmployeesNode.appendChild(textNode);
	    root.appendChild(nrOfEmployeesNode);
	    	    
	    Source result = new DOMSource(root);
	    return result;
	}
	

}
